from Autotests.LocalMachine.Pages.BasePage import BaseWebPage
from Autotests.LocalMachine.UiObjectsMap.AirClient import MainFrame, Reports
from Autotests.LocalMachine.Controls.BaseControl import WebFindKey
from Autotests.LocalMachine.Controls.Label import WebLabel
from Autotests.LocalMachine.Controls.Button import WebButton
from Autotests.LocalMachine.Controls.ListView import WebListView, WebListViewDelegateBuilder
from Autotests.LocalMachine.Controls.TableView import WebTableView
from Autotests.LocalMachine.Controls.BaseView import BaseWebViewLeafDelegate, WebViewDelegateIndex
import time


class WebReportsListDelegate(BaseWebViewLeafDelegate):
    def __init__(self, handler, index):
        super(WebReportsListDelegate, self).__init__(handler, index)
        self._dateLabel = WebLabel(self.waitChildControl(WebFindKey.XPATH, 'h3/p/span[1]'))
        self._backupDataLabel = WebLabel(self.waitChildControl(WebFindKey.XPATH, 'h3/p/span[2]'))
        self._reportsTable = WebTableView(self.waitChildControl(WebFindKey.TAG_NAME, 'table'))

    def setSelection(self, selection):
        pass

    def isSelected(self):
        return False

    def date(self):
        return self._dateLabel.text()

    def backupData(self):
        return self._backupDataLabel.text()

    def reportsTable(self):
        if not self._reportsTable.isVisible():
            self._dateLabel.click()
        return self._reportsTable

    def viewDetails(self, row):
        table = self.reportsTable()
        time.sleep(1)
        table.row(row).click()

    def isInfoAvailable(self):
        return True


class WebReportsListDelegateBuilder(WebListViewDelegateBuilder):
    def __init__(self):
        childHandler = (WebFindKey.XPATH, "./div")
        super(WebReportsListDelegateBuilder, self).__init__(childHandler)

    def build(self, controlHandler, index=WebViewDelegateIndex()):
        if not index.isValid():
            return super(WebReportsListDelegateBuilder, self).build(controlHandler, index)
        return WebReportsListDelegate(controlHandler, index)


class WebReportsPage(BaseWebPage):
    def __init__(self):
        super(WebReportsPage, self).__init__(Reports.reportsPage)
        self._homeButton = WebButton(MainFrame.btnReports)
        self._reportsList = WebListView(Reports.reportsListView, WebReportsListDelegateBuilder())

    def goToHomeLocation(self):
        super(WebReportsPage, self).goToHomeLocation()
        self._homeButton.click()

    def _getDateDelegate(self, date):
        return self._reportsList.itemsFor(lambda x: x.date() == date)[0]

    def getReportsCountByDate(self, date):
        self.goToHomeLocation()
        reports = self._getDateDelegate(date)
        return reports.reportsTable().rowsCount()

    def viewReportInfo(self, date='', row=0):
        self.goToHomeLocation()
        if self._reportsList.rowsCount() > 0:
            if date == '':
                self._reportsList.items()[0].viewDetails(row)
            else:
                self._getDateDelegate(date).viewDetails(row)

    def viewReport(self, date=''):
        self.goToHomeLocation()
        if self._reportsList.rowsCount() > 0:
            if date == '':
                self._reportsList.items()[0].reportsTable()
            else:
                self._getDateDelegate(date).reportsTable()